﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OALPTool
{
    public partial class DataGridViewDemoEX : Form
    {
        
        #region 自定义变量

        /// <summary>
        /// DataGridView数据源
        /// </summary>
        private DataView dv = new DataView();

        #endregion

        #region 界面初始化

        public DataGridViewDemoEX()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化界面
        /// </summary>
        /// <returns></returns>
        public int InitForm()
        {
            this.InitTable();

            this.SetDgvStyle();

            return 0;
        }

        #endregion

        #region 数据源初始化

        /// <summary>
        /// 初始化DataGridView数据源
        /// </summary>
        public void InitTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("departname", typeof(String));
            dt.Columns.Add("year", typeof(String));
            dt.Columns.Add("month", typeof(String));
            dt.Columns.Add("userno", typeof(String));
            dt.Columns.Add("username", typeof(String));
            dt.Columns.Add("amt", typeof(Double));

            DataRow dr = dt.NewRow();
            dr["departname"] = "总经理办公室";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000001";
            dr["username"] = "徐予华";
            dr["amt"] = 1000;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "总经理办公室";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "陈立文";
            dr["amt"] = 1000;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "部门小计";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "";
            dr["username"] = "";
            dr["amt"] = 2000;
            dt.Rows.Add(dr);     

            dr = dt.NewRow();
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "蔡宁";
            dr["amt"] = 800;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "丁则然";
            dr["amt"] = 800;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "顾建国";
            dr["amt"] = 800;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "王晓琴";
            dr["amt"] = 800;
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["departname"] = "财务部";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "1000002";
            dr["username"] = "傅伯文";
            dr["amt"] = 800;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "部门小计";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "";
            dr["username"] = "";
            dr["amt"] = 4000;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["departname"] = "总计";
            dr["year"] = "2009";
            dr["month"] = "6";
            dr["userno"] = "";
            dr["username"] = "";
            dr["amt"] = 6000;
            dt.Rows.Add(dr);  

            dt.AcceptChanges();

            this.dv = dt.DefaultView;

        }

        #endregion

        #region DataGridView操作

        /// <summary>
        /// 设置DataGridView样式
        /// </summary>
        private void SetDgvStyle()
        {
            DataGridViewFunc.setDgvStyle(this.getColList(), ref this.dgv, this.dv);

            this.dgv.AutoGenerateColumns = false;
            this.dgv.AllowUserToAddRows = false;
            this.dgv.AllowUserToDeleteRows = false;

            this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //不可排序
            foreach (DataGridViewColumn dgCol in this.dgv.Columns)
            {
                dgCol.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        /// <summary>
        /// 获取DataGridView样式列表
        /// </summary>
        /// <returns>返回样式列表</returns>
        private ArrayList getColList()
        {

            ArrayList colList = new ArrayList();
            ///   0:字段中文名；
            ///   1:字段英文名；
            ///   2:值类型【0-TextBox(默认),1-CheckBox,2-ComboBox,3-DateTimePicker,6-TextBox(自动换行),7-超链接】；
            ///   3:显示宽度；
            ///   4:显示格式化【"c"表示数字格式化；"d"表示时间格式化】；
            ///   5:对齐方式；
            ///   6:是否只读【true为只读；false为可修改】；
            ///   7:数据为空时显示；
            ///   8:是否存在转义；
            ///   9:显示顺序号；
            ///   10:颜色字段名【暂时未用】；
            ///   11:颜色字段意义列表【暂时未用】；
            ///   12:格式化字段；
            ///   13:暂时没用；
            ///   14:如果值类型为2，那么该值为Combol项集合对应数据集【每条数据对应一个object集合，object[0]为Display值，object[1]为Value值】            
            colList.Add(new object[] { "部门", "departname", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 1, "", "", "", "", "" });
            colList.Add(new object[] { "工号", "userno", "0", 80, null, DataGridViewContentAlignment.MiddleCenter, true, "", null, 2, "", "", "", "", "" });
            colList.Add(new object[] { "姓名", "username", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 3, "", "", "", "", "" });
            colList.Add(new object[] { "津贴", "amt", "0", 100, "c", DataGridViewContentAlignment.MiddleRight, true, "", null, 4, "", "", "#,##0.00", "", "" });

            return colList;
        }

        /// <summary>
        /// 重绘DataGridView  显示序号
        /// </summary>
        private void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //显示DataGridView序号
            DataGridViewFunc.disDgvSerialno(this.dgv, e);
        }

        /// <summary>
        /// 重绘行颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //颜色设置  0-列号，1-前景色，2-变色条件，当该列号值为该条件时进行变色，3-背景色
            ArrayList colorList = new ArrayList();
            colorList.Add(new object[] { 0, Color.Red.ToArgb(), "部门小计", Color.White.ToArgb() });
            colorList.Add(new object[] { 0, Color.Red.ToArgb(), "总计", Color.BlueViolet.ToArgb() });
           
            //整行变色 "0"表示整行变色，"1"表示只该单元格变色
            DataGridViewFunc.setDgvColor(this.dgv, e, 0, colorList, "0");
            //只单元格变色
            //DataGridViewFunc.setDgvColor(this.dgv, e, 0, colorList, "1");

        }

        #endregion

        #region 界面事件

        private void tbbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

    }
}
